package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AddressServiceImpl implements AddressService {

    //依赖注入
    @Autowired
    private AddressMapper addressMapper;

    //查询现有数据
    @Override
    public List<Address> findList() {

        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        //构建查询条件,address表中的userid与登录是记录的id进行比较
        wrapper.eq(Address::getUserId, UserHolder.get().getId());
        //执行查询返回数据
        return addressMapper.selectList(wrapper);
    }

    //添加地址
    @Override
    public void save(Address address) {

        //填充其他字段,设置为登录时id
        address.setUserId(UserHolder.get().getId());
        address.setIsDefault(0);//0：不是默认地址

        //调用mapper层添加
        addressMapper.insert(address);
    }

    //修改默认地址
    @Override
    public void setDefault(Long id) {

        //先将当前所有地址修改为非默认地址
        Address address = new Address();
        address.setIsDefault(0);

        //构建更新条件
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUserId,UserHolder.get().getId());
        //执行更新
        addressMapper.update(address,wrapper);

        //将现有选择更新为默认地址
        Address address1 = new Address();
        address1.setId(id);
        address1.setIsDefault(1);

        //调用mapper修改
        addressMapper.updateById(address1);
    }

    //查询默认地址
    @Override
    public Address findDefault() {

        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();

        //构建查询条件(where userId = #{id} and isDefault = 1)
        wrapper.eq(Address::getUserId,UserHolder.get().getId())
                .eq(Address::getIsDefault,1);

        return addressMapper.selectOne(wrapper);
    }

    //修改数据前回显数据
    @Override
    public Address findById(long id) {

        //构建查询条件
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getId,id);

        //调用mapper层查询
        return addressMapper.selectOne(wrapper);
    }


    //修改地址数据
    @Override
    public void update(Address address) {
        //构建更新条件
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getId,address.getId());

        Address address1 = addressMapper.selectOne(wrapper);

        //先查询这条数据，如果为默认地址，则提示更改完默认地址之后修改
        if (address1.getIsDefault() == 1){
            throw new CustomException("当前地址为默认地址，请更改默认地址之后修改地址");
        }
        // 执行修改
        addressMapper.update(address,wrapper);
    }

    //删除地址数据
    @Override
    public void deleteById(Long ids) {

        //构建条件
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getId,ids);

        //先查询数据，判断是否是默认地址
        Address address = addressMapper.selectOne(wrapper);
        if (address.getIsDefault() == 1){
            throw new CustomException("当前地址是默认地址，请重新选择一个之后删除该地址");
        }
        //删除该地址
        addressMapper.delete(wrapper);
    }
}
